package cn.bluethink.mouse;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;

import cn.bluethink.view.Graph;
import cn.bluethink.view.GraphView;

public class MouseSelectOp implements IMouseOperator {

	public final static int NONE = 0;
	public final static int MOVE = 1;
	public final static int NODE = 2;
	
	protected int mSelectMode = NONE;
	private int mHandleIndex ;
	private Point mPrePoint;
	private GraphView mGraphView;
	
	public MouseSelectOp(GraphView view) {
		this.mGraphView = view;
	}
	@Override
	public void mouseClick(MouseEvent event) {
	}

	@Override
	public void mouseReleased(MouseEvent event) {
		mSelectMode = NONE;
		mGraphView.repaint();
	}

	@Override
	public void mouseEntered(MouseEvent event) {
	}

	@Override
	public void mousePressed(MouseEvent event) {
		mSelectMode = NONE;
		mHandleIndex = 0;
		mPrePoint = event.getPoint();
		mGraphView.mSelectedGraphs.clear();
		if(mGraphView == null) {
			return;
		}
		for(Graph g : mGraphView.mGraphs) {
			int handleIndex = g.hitTest(event.getPoint());
			if(handleIndex != 0) {
				mGraphView.mSelectedGraphs.add(g);
				mHandleIndex = handleIndex;
			}
		}

		int sel_count = mGraphView.mSelectedGraphs.size();

		if (sel_count > 1){
			mSelectMode = NONE;
			return;
		}

		if ( sel_count == 0 ){
			return;
		}

		if ( sel_count == 1 && mHandleIndex > 0 ){
			mSelectMode = NODE;
		}else {
			mSelectMode = MOVE;
		}

	}

	@Override
	public void mouseExited(MouseEvent event) {
	}

	@Override
	public void mouseDragged(MouseEvent event) {
		if ( mSelectMode == NONE ){
			return;
		}
		Graphics g = mGraphView.getGraphics();
		if (mSelectMode == NODE) {
			Graph graph = mGraphView.mSelectedGraphs.get(0);
			graph.draw(g);
			mGraphView.refresh();
			graph.moveHandleTo(mHandleIndex, new Point(event.getX(), event.getY()));
			graph.draw(g);

		} else{
			for (Graph graph : mGraphView.mSelectedGraphs) {
				graph.draw(g);
			}
			mGraphView.refresh();
			for (Graph graph : mGraphView.mSelectedGraphs) {
				graph.moveTo(event.getX() - mPrePoint.x, event.getY() - mPrePoint.y);
				graph.draw(g);
			}
			mPrePoint.setLocation(event.getX(), event.getY());
		}
	}

	@Override
	public void mouseMoved(MouseEvent event) {
	}

}
